package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2016092200568607";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCHB2IfhS52eHia1JyqynZOrrnebsZMzobUuTfO47KNzeIrbSzaOnN6r/QC5h4aIJNtkRnTBq635uCU/YE/MUy6KhxisJpiuB8g+WVv2cfV/37XxTgqOwODLItTVmOXwmnH6lclMyj7bm1XdRR8jX5t7eIGGj0/8Id0Nhabgr34gXQE/W3+aWhBXOTPfGkc8CGQa6ioFdjkNzPsK2HXAIOQRZkF4MHJKdKpv8Cki7rBib2LLThgM5SU0UPZfq4NzpMvStZq+8pXRNjI4tWKwmamnTLvBV04MdsCWf5HwZLuR/C3OmxXJhv1m5dONgT1XDSbCqZG/XfVWb6z+BMvvjerAgMBAAECggEAe5xDtG08YpHYfoziOuuLMG59kuW/vPJXtgc00r3ZaD0wreaM594Ydxj6fWjh57Moo2XwySFV8t51e48ysYHVJpuETsgzHOpG/O2ycZlhekMhgC94QhiyF3RAu3LbS0NbiPq6+kACZWHq0ORKc9coBUR+5G1C4vSzW6xkN8qRVBsAHTMrjlTI7/BV3R/kVkxr7p504f2yrBlcPzg+gh1kFPnVR0ZWyExGyUbHQw70zejdIqrgJdK3oPmLDEFJCzCClH3d3cOr3mkafuzSRiQHCHkrOgiMje5AZ5bIaioKZXOf/F0w8FB9u/UI5aawO3jF0nX8VFvdfGbTRYgb9KGr8QKBgQDL0tzQUBZcD3+U1tsYxfK30MOUf/zyphKBjWsvTSXKRi7fdhHzYRUqBWuRXjL4+ni7m0fNL5Pr48de8x1rEcviGwtDIicCn7TqXlHS3qV1G98yF05S3E392B5wkWkfD73GgZ+LXGB9ViLdpMCESBBzt6jGl8za6Xqg+lprYQnJMwKBgQCpmDOvsbYB3HnYPJIZlI42p7iereiaNrxUA+ddlkJRVat59zDd0Psdsl+cBULJYLO1fKKj/msgLqrmzuJgT0Qrj0ghEhiFS9U08h2XRYLfl70ars/xzwhBhHmfJK87oLo/gqqqX1F3gD8an3IxInsq2I2uDrGM3Qm9pJuQBRgHqQKBgQDFnbLz0cJsDaa/8zTIJxv2NlV6BEGGLZghvbBfUyhBnpxRklNDKixv6oNplPBMz+QA2UPbX7bGbbjeiU5LghywI1JfC5zRAXFSDpK8pZu8GILlOQ16bxi69jBFKebG8+cmsIS0hKMUwk/Zn6P8XTZ+S2I68fDvipGXxEOoepQXWwKBgE8DJwly7Q3+7xFsXtDmIj+acmwTL+wxFhq6Wt+CMtF77VD8dlrRkzzprpuc49m4YOFr+MYYsUYkF+FCf7tJEsSMk5k8ugnYI7PwHRMuAmQzaJn22dCZmQoVMyiOycko/7y5HtQTZnwbVssLgAMeQm3ayF9QXy32iSQ+fW9iwChJAoGAHpgmpgR67FSjuKjsyXinC2jmowiP510y+2earkE0a9EJ02F6ozQWgdKwbyl0FhORIU9TgcVWMZJ1ywm4jsJAijJv7Ay8jxcALeapDqkTPqbTOjUvT0RLf5aRP8QNlbruiP/h89C8cUh5wvbQk7zWJh766IawbCsYFsn6l+PcuTw=";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwt4lwz16MLKSmB59VpPqrQ/0h8A9yv5bXw2YRgfgoCjru/hcKuagiXkGdfHgmvvvhokNFFTxGwUosHt21hPvdj8GsgZ1OvljbTGrrY0p24LxCe+0tk6hdnXcng2IGat1YHsKGqQSAfvlxOnw4YFtVjJ8RrpIj/kfHtagEDnLdqIBQmA4FHSOPLPb/UwT5RVMyfIJsvnIyIf50sONPORqHd6HUUfnt5YctHe0RAy4HEde1A/DOOK4CED2xUSlKfo6q3l7H+/Gh1AXFS/Iv1zk9pcj5GMuD/2yFUY6MpLGaL9bD/hlmZ9XZ6PwfVsOP9ZN/3OXJgJi5QTigauwciyVEQIDAQAB";

    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="http://cjxhyt.natappfree.cc/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
